<template>
  <v-dialog
    :title="title"
    :close-on-click-modal="false"
    v-model="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'" label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="采购id" prop="purchase.id"
                :rules="[
                 ]">
          <GridSelect
            title="选择采购id"
            labelName = 'purchaseName'
            labelValue = 'id'
            v-model = "inputForm.purchase.id"
            :limit="1"
            :columns="[
            {
              prop: 'purchaseNo',
              label: '采购编号'
            },
            {
              prop: 'purchaseName',
              label: '采购名称'
            }
            ]"
            :searchs="[
            {
              prop: 'purchaseNo',
              label: '采购编号'
            },
            {
              prop: 'purchaseName',
              label: '采购名称'
            }
            ]"
            :api="purchaseService">
          </GridSelect>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="合同类型" prop="contractType"
                :rules="[
                 ]">
                <el-select v-model="inputForm.contractType" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('contract_type')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="数量" prop="contractCount"
                :rules="[
                 ]">
          <el-input-number v-model="inputForm.contractCount" placeholder="请填写数量"     style="width: 100%;"></el-input-number>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <template #footer>    
        <span class="dialog-footer">
          <el-button @click="visible = false" icon="circle-close">关闭</el-button>
          <el-button  type="primary" v-if="method != 'view'" @click="doSubmit()" icon="circle-check" v-noMoreClick>确定</el-button>
        </span>
    </template>    
  </v-dialog>
</template>

<script>
  import GridSelect from '@/components/gridSelect'
  import predictContractService from '@/api/predict/predictContractService'
  import purchaseService from '@/api/purchase/purchaseService'   
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        purchaseService: purchaseService,  
        inputForm: {
          id: '',
          purchase: {
            id: ''
          },
          contractType: '',
          contractCount: ''
        }
      }
    },
    components: {
      GridSelect
    },
    created () {
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建预计合同`
        } else if (method === 'edit') {
          this.title = '修改预计合同'
        } else if (method === 'view') {
          this.title = '查看预计合同'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            predictContractService.queryById(this.inputForm.id).then((data) => {
              this.inputForm = this.recover(this.inputForm, data)
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            predictContractService.save(this.inputForm).then((data) => {
              this.visible = false
              this.$message.success(data)
              this.$emit('refreshDataList')
              this.loading = false
            }).catch(() => {
              this.loading = false
            })
          }
        })
      }
    }
  }
</script>

  
